<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Models_Mysql_Hook extends Modules_Core_Models_Hook
{
	protected $_table = 'core_hook';
	
	public function dbHooks($module = null, $all = false)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . $this->_table;
		if ($module != null) {
			$sql .= ' WHERE module = "' . $this->_conn->escape($module) . '"';
		}
		$sql .=  ' ORDER BY name ASC';
		$rs = $this->_conn->query($sql);
		$rows = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$rows[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $rows;
	}
	
	public function getHooks($module = null)
	{
		$sql  = "SELECT * FROM " . $this->_conn->_tablePrefix . $this->_table;
		if ($module) {
			$sql .= " WHERE module = '" . $this->_conn->escape($module) . "'";
		}
		$sql .= "ORDER BY name ASC";
		$rs = $this->_conn->query($sql);
		$rows = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$rows[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $rows;
	}
	
	public function getModules()
	{
		$sql  = "SELECT DISTINCT module FROM " . $this->_conn->_tablePrefix . $this->_table . " ORDER BY module ASC";
		$rs = $this->_conn->query($sql);
		$rows = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$rows[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $rows;
	}
	
	public function add($hook)
	{
		$this->_conn->insert($hook, $this->_conn->_tablePrefix . $this->_table);
	}
	
	public function exist($hook) 
	{
		$sql = sprintf("SELECT COUNT(*) AS num_hooks FROM " . $this->_conn->_tablePrefix . $this->_table . " WHERE name = '%s'", 
						$this->_conn->escape($hook['name']));
		if ($hook['module'] && $hook['module'] != '') {
			$sql .= sprintf(" AND module = '%s'", $hook['module']);
		} else {
			$sql .= " AND module IS NULL";
		}
		$sql .= " LIMIT 1";
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return ($row['num_hooks']) ? true : false;
	}
	
	public function delete($id) 
	{
		$sql = sprintf("DELETE FROM " . $this->_conn->_tablePrefix . $this->_table . " WHERE hook_id = '%s'", $this->_conn->escape($id));
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
	
	public function install($hook) 
	{
		$id = $this->add($hook);
				
		/**
		 * Perform the action when hook is activated
		 */
		$hookClass = (null == $hook['module'] || '' == $hook['module']) 
					? 'Hooks_' . $hook['name'] . '_Hook'
					: $hook->module . '_Hooks_' . $hook['name'] . '_Hook';
		/**
		 * TODO: Make this as service
		 */
		if (class_exists($hookClass)) {
			$hookInstance = new $hookClass();
			if ($hookInstance instanceof Gio_Core_Hook) {
				$hookInstance->activate();
			}
		}
		
		return $id;
	}
	
	public function uninstall($hook) 
	{
		/**
		 * Delete hook
		 */
		$this->delete($hook['hook_id']);
		
		/**
		 * Perform the action when hook is deactivated
		 * TODO: Make this as service
		 */
		$hookClass = (null == $hook['module'] || '' == $hook['module']) 
					? 'Hooks_' . $hook['name'] . '_Hook'
					: $hook['module'] . '_Hooks_' . $hook['name'] . '_Hook';
		if (class_exists($hookClass)) {
			$hookInstance = new $hookClass();
			if ($hookInstance instanceof Gio_Core_Hook) {
				$hookInstance->deactivate();
			}
		}
		
		/**
		 * Remove hook from targets if any
		 */
		$sql = sprintf("DELETE FROM " . $this->_conn->_tablePrefix . $this->_table . " WHERE hook_name = '%s'", 
						$this->_conn->escape($hook['name']));
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}	
}
